package jmotor.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.security.Permission;

/**
 * Component:
 * Description:
 * Date: 11-8-18
 *
 * @author Andy.Ai
 */
public class FileUtils {
    private static final SecurityManager securityManager = new SecurityManager() {
        public void checkPermission(Permission perm) {
        }
    };
    private static final SecurityManager defSecurityManager = System.getSecurityManager();

    private FileUtils() {
    }

    public static InputStream loadFileStream(String path) throws FileNotFoundException {
        return loadFileStream(new File(path));
    }

    public static InputStream loadFileStream(File file) throws FileNotFoundException {
        if (file.exists() && !file.isDirectory()) {
            return new FileInputStream(file);
        }
        throw new FileNotFoundException(file.getPath() + ":Can't found or it's directory");
    }

    public static byte[] readFile(String path) throws IOException {
        InputStream inputStream = loadFileStream(path);
        try {
            return StreamUtils.transform(inputStream);
        } finally {
            inputStream.close();
        }
    }

    public static void writeFile(String path, byte[] content) throws IOException {
        writeFile(path, content, false);
    }

    public static void writeFile(String path, byte[] content, boolean append) throws IOException {
        File file = new File(path);
        File parentDir = file.getParentFile();
        boolean writable = true;
        if (!parentDir.exists()) {
            writable = parentDir.mkdirs();
        }
        if (writable) {
            FileOutputStream fileOutputStream = new FileOutputStream(file, append);
            try {
                fileOutputStream.write(content);
                fileOutputStream.flush();
            } finally {
                fileOutputStream.close();
            }
        }
    }

    public static boolean deleteFile(String path) {
        return deleteFile(new File(path));
    }

    public static boolean deleteFile(File file) {
        return deleteFile(file, null);
    }

    public static boolean deleteFile(File file, FilenameFilter filenameFilter) {
        System.setSecurityManager(securityManager);
        if (file.isDirectory()) {
            File[] listFiles;
            if (filenameFilter == null) {
                listFiles = file.listFiles();
            } else {
                listFiles = file.listFiles(filenameFilter);
            }
            for (File fileChild : listFiles) {
                deleteFile(fileChild, filenameFilter);
            }
        }
        boolean successful = file.delete();
        System.setSecurityManager(defSecurityManager);
        return successful;
    }
}
